<?php
namespace Operationtest\Model;

use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\AbstractTableGateway;

class RhdataTable extends AbstractTableGateway {
    protected $table ='RhData';
    
    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Rhdata());
        $this->initialize();
    }
    
    public function getOldRhdata($idStore, $date) {
        $sql = 'SELECT date'
                . ' FROM '.$this->table
                . ' WHERE date = "'.$date.'" AND idStore = '.$idStore;
        $result = $this->adapter->query($sql)->execute();
        $return = $result->current();
        return $return;
    }
    
    public function saveRhdata($idStore, $date, $rhData) {
        $oldRhdata = $this->getOldRhdata($idStore, $date);
        if(!$oldRhdata) {
            $data = array(
                'idRhdata' => '',
                'date' => $date,
                'idStore' => $idStore,
                'MO' => $rhData['mo'],
                'transfertE' => $rhData['te'],
                'transfertS' => $rhData['ts'],
                'nbHeures' => $rhData['heure'],
                'productivite' => $rhData['productivite']
            );
            $this->insert($data);            
        } else {
            $data = array(
                'MO' => $rhData['mo'],
                'transfertE' => $rhData['te'],
                'transfertS' => $rhData['ts'],
                'nbHeures' => $rhData['heure'],
                'productivite' => $rhData['productivite']
            );
            $this->update($data, array('date' => $date, 'idStore' => $idStore));
        }
    }
    
    public function getTbRhdata($where) {
        $sql = "SELECT 
                    SUM(MO) as mo,
                    SUM(transfertE) as te,
                    SUM(transfertS) as ts,
                    SUM(nbHeures) as nbh,
                    SUM(productivite) as prod,
                    date
                FROM ".$this->table."
                WHERE ".$where;
        //echo $sql.'<br />';
        $result = $this->adapter->query($sql)->execute();
        $return = $result->current();

        return $return;
    }
    
    public function getSynthRhdata($where, $groupby="") {
        $rows = array();
        $sql = "SELECT 
                    SUM(MO) as mo,
                    SUM(transfertE) as te,
                    SUM(transfertS) as ts,
                    SUM(nbHeures) as nbh,
                    SUM(productivite) as prod,
                    date,
                    idStore
                FROM ".$this->table."
                WHERE ".$where.$groupby;
        
        $result = $this->adapter->query($sql)->execute();
        for($x=0;$x<count($result);$x++):
            $rows[$x] = $result->current();
        endfor;
        
        return $rows;
    }
    
    public function getBgMO($where, $groupby="") {
        $rows = array();
        $sql = "SELECT 
                    SUM(MO) as mo,
                    SUM(transfertE) as te,
                    SUM(transfertS) as ts,
                    SUM(nbHeures) as nbh,
                    SUM(productivite) as prod,
                    idStore
                FROM ".$this->table."
                WHERE ".$where.$groupby;
        
        $result = $this->adapter->query($sql)->execute();
        for($x=0;$x<count($result);$x++):
            $rows[$x] = $result->current();
        endfor;
        
        return $rows;
    }
    
    public function getBgMarcheMO($where) {
        $rows = array();
        $sql = "SELECT 
                    SUM(MO) as mo,
                    SUM(transfertE) as te,
                    SUM(transfertS) as ts,
                    SUM(nbHeures) as nbh,
                    SUM(productivite) as prod,
                    idMarche
                FROM ".$this->table."
                JOIN MarcheStore
                    ON (MarcheStore.idStore = RhData.idStore)
                WHERE ".$where." GROUP BY idMarche";
        //echo $sql;
        $result = $this->adapter->query($sql)->execute();
        for($x=0;$x<count($result);$x++):
            $rows[$x] = $result->current();
        endfor;
        
        return $rows;
    }
}
?>
